package com.navinfo.tripanalysis.common.arithmetic;

import com.navinfo.tripanalysis.common.arithmetic.annotation.Arithmetic;
import com.navinfo.tripanalysis.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.tripanalysis.common.util.ArithmeticUtils;
import com.navinfo.tripanalysis.common.arithmetic.common.CommonData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;
import com.navinfo.tripanalysis.common.arithmetic.custom.VarClutchSwitch;
import com.navinfo.tripanalysis.common.arithmetic.enums.EventStatus;

/**
 * 离合开关算法.<br>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 输出：统计<br/>
 */
@Arithmetic(name = "离合开关算法")
public class ClutchSwitch extends AbstractArithmetic<VarClutchSwitch> {
    /**
     * 离合器是否开启
     * @param clutchSwitch
     * @return
     */
    private boolean isClutchOpen(int clutchSwitch) {
        return clutchSwitch == 1;
    }

    @Override
    public void calculate() {
        logger.debug("进入离合器开关时长算法.");
        VarClutchSwitch var = getObj();
        CommonData data = getCommonData();

        boolean preStatus = var.isPreClutchOpen();
        boolean curStatus = isClutchOpen(data.getClutchSwitch());
        logger.debug("离合器开关算法，离合器开关状态：pre:{},current:{}", preStatus, curStatus);

        EventStatus eventStatus = super.getEventStatus(preStatus, curStatus);
        switch (eventStatus) {
            case START:
                logger.debug("离合器开关算法，事件开始.");
                var.setNumber(var.getNumber() + 1);
                break;
            case IN:
                logger.debug("离合器开关算法，事件中.");
                var.setMileage(var.getMileage() + data.getDffDistance());
                var.setTimeMs(var.getTimeMs() + data.getDffTime());
                break;
            case END:
                logger.debug("离合器开关算法，事件结束.");
                var.setMileage(var.getMileage() + data.getDffDistance());
                var.setTimeMs(var.getTimeMs() + data.getDffTime());
                break;
            default:
                logger.debug("普通刹车统计，状态：上一次和本次都未开启离合器，跳出算法.");
                break;
        }

        //设置上一个点状态、供下个点计算
        var.setPreClutchOpen(curStatus);
        logger.debug("离合器开关时长算法结束.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarClutchSwitch var = super.getObj();
        statisticDataOut.setClutchNumber(var.getNumber());
        statisticDataOut.setClutchMileage(ArithmeticUtils.mileageMToM(var.getMileage()));
        statisticDataOut.setClutchDuration(ArithmeticUtils.timeMsToSecond(var.getTimeMs()));
    }
}
